const Axios = require("axios").default

import Vue from "../main";


const network = Axios.create({
    baseURL: "/api",
    timeout: 10000,
})

const http = info => {
    return new Promise((reso, reje) => {
        network({
            url: info.url,
            method: info.method ? info.method : "GET",
            data: info.data ? info.data : {},
            params: info.params ? info.params : {}
        }).then(r => {
            reso(r.data)
        }).catch(e => {
            console.log(e);
            let resp = e.response
            if (resp.data.code == 401) {
                Vue.$message.error("请先登陆!")
                setTimeout(() => {
                    Vue.$router.push('/login')
                }, 1000);
                return
            } else {
                Vue.$message.error("网络异常!")
                // reje(e)
            }
        })
    })
}

network.interceptors.request.use(config => {
    let token = window.localStorage.getItem("token")
    if (token)
        config.headers.Authorization = token
    return config
})

export default http